package io.renren.modules.testrestful.controller;

import io.renren.modules.testrestful.service.impl.UsersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.*;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;


public class websocketClient {

    @Autowired
    UsersServiceImpl usersService;
//    @Scheduled(cron = "0/5 * * * * ?")
    public void webSocketClient() throws InterruptedException {
        String token = usersService.getToken().toString();
        StandardWebSocketClient standardWebSocketClient = new StandardWebSocketClient();
        WebSocketConnectionManager webSocketConnectionManager = new WebSocketConnectionManager(standardWebSocketClient, new PrintClientSocketHandler(),
                "wss://thingsboard.cmhijs.cn/api/ws/plugins/telemetry?" +
                        "token=" + token);

        webSocketConnectionManager.start();

        Thread.sleep(10000L);
    }



    public static class PrintClientSocketHandler extends AbstractWebSocketHandler {

        //连接成功,发送信息
        String sendStr = "{\n" +
                "    \"tsSubCmds\":[\n" +
                "        {\n" +
                "            \"entityType\":\"DEVICE\",\n" +
                "            \"entityId\":\"6c3a1cd0-da75-11ed-bbb0-dde55e1f139a\",\n" +
                "            \"scope\":\"LATEST_TELEMETRY\",\n" +
                "            \"cmdId\":10,\n" +
                "            \"keys\":\"DDE5.Gruppe_1.AxisPositionX,DDE5.Gruppe_1.AxisPositionY\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"historyCmds\":[\n" +
                "    ],\n" +
                "    \"attrSubCmds\":[\n" +
                "\n" +
                "    ]\n" +
                "}\n";

        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            session.sendMessage(new TextMessage(sendStr));
        }

        @Override
        protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//            System.out.println("收到消息：" + message.getPayload());
        }

        @Override
        protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
//        session.sendMessage(new TextMessage(StandardCharsets.UTF_8.decode(message.getPayload()).toString()));
        }

        @Override
        protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
//        session.sendMessage(new TextMessage(StandardCharsets.UTF_8.decode(message.getPayload()).toString()));
        }

        @Override
        public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {

        }

        @Override
        public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {

        }
    }
}